#include "stdafx.h"
#include "DataManager.h"


CDataManager::CDataManager()
{
}


CDataManager::~CDataManager()
{
	for (AccountInfoPos pos = AccountMap.begin(); pos != AccountMap.end(); ++pos)
	{
		CAccountInfo* curAcc = pos->second;
		delete curAcc;
		curAcc = NULL;
	}

	AccountMap.clear();

	for (size_t i = 0; i < SymbolList.size(); i++)
	{
		CSymbolInfo* curSym = SymbolList[i];
		delete curSym;
		curSym = NULL;
	}

	SymbolList.clear();


	for (SymbolInfoPos pos = SymbolMap.begin(); pos != SymbolMap.end(); ++pos)
	{
		CSymbolInfo* curSym = pos->second;
		delete curSym;
		curSym = NULL;
	}

	SymbolMap.clear();

	for (size_t i = 0; i < AccountList.size(); i++)
	{
		CAccountInfo* curAcc = AccountList[i];
		delete curAcc;
		curAcc = NULL;
	}

	AccountList.clear();

	
	
	
}

CSymbolInfo* CDataManager::AddSymbolInfo(wstring a_SymBolCode)
{
	size_t size = SymbolMap.size();
	if (size == 0)
	{
		CSymbolInfo* symInfo = new CSymbolInfo();
		symInfo->m_Code = a_SymBolCode;
		SymbolMap.insert(pairSymbolInfo(a_SymBolCode, symInfo));
		return symInfo;
	}
	else
	{
		SymbolInfoPos pos = SymbolMap.find(a_SymBolCode);
		if (pos == SymbolMap.end())
		{
			CSymbolInfo* symInfo = new CSymbolInfo();
			symInfo->m_Code = a_SymBolCode;
			SymbolMap.insert(pairSymbolInfo(a_SymBolCode, symInfo));
			return symInfo;
		}
		else
		{
			return pos->second;
		}
	}
}

CAccountInfo* CDataManager::AddAccountInfo(wstring a_AccName, wstring a_HipendCode)
{
	size_t size = AccountMap.size();
	if (size == 0)
	{
		CAccountInfo* accInfo = new CAccountInfo();
		accInfo->Code = a_AccName.c_str();
		accInfo->HipenCode = a_HipendCode.c_str();
		AccountMap.insert(pairAccountInfo(a_AccName, accInfo));
		return accInfo;
	}
	else
	{
		AccountInfoPos pos = AccountMap.find(a_AccName);
		if (pos == AccountMap.end())
		{
			CAccountInfo* accInfo = new CAccountInfo();
			accInfo->Code = a_AccName.c_str();
			accInfo->HipenCode = a_HipendCode.c_str();
			AccountMap.insert(pairAccountInfo(a_AccName, accInfo));
			return accInfo;
		}
		else
		{
			return pos->second;
		}
	}
}

CSymbolInfo* CDataManager::FindSymbolInfo(wstring a_SymBolCode)
{
	SymbolInfoPos pos = SymbolMap.find(a_SymBolCode);
	if (pos == SymbolMap.end())
	{
		return NULL;
	}
	else
	{
		return pos->second;
	}
}

CAccountInfo* CDataManager::FindAccountInfo(wstring a_AccName)
{
	AccountInfoPos pos = AccountMap.find(a_AccName);
	if (pos == AccountMap.end())
	{
		return NULL;
	}
	else
	{
		return pos->second;
	}
}

CAccountInfo* CDataManager::FindAccountByHipenedCode(wstring a_AccCode)
{
	CAccountInfo* curAcc = NULL;
	for (AccountInfoPos pos = AccountMap.begin(); pos != AccountMap.end(); ++pos)
	{
		curAcc = pos->second;
		if (curAcc->HipenCode.CompareNoCase(a_AccCode.c_str()) == 0)
		return curAcc;
	}

	return NULL;
}

void CDataManager::AddTickInfo( CSymbolInfo* a_SymInfo )
{
	size_t size = dqTickInfo.size();
	TickInfo curTick;
	curTick.AccVolume = a_SymInfo->m_TickInfo.AccVolume;
	curTick.LSType = a_SymInfo->m_TickInfo.LSType;
	curTick.OpenInterest = a_SymInfo->m_TickInfo.OpenInterest;
	curTick.Price = a_SymInfo->m_TickInfo.Price;
	curTick.Time = a_SymInfo->m_TickInfo.Time;
	curTick.Volume = a_SymInfo->m_TickInfo.Volume;
	curTick.oleTime = a_SymInfo->m_TickInfo.oleTime;

	COleDateTime oleTime;
	//oleTime.SetTime();
	if (size > 300)
	{
		// Remove the oldest item in the deque;
		dqTickInfo.pop_back();
		// Add the recent item on the deque;
		dqTickInfo.push_front(curTick);
	}
	else
	{
		// Just add the item on the deque;
		dqTickInfo.push_front(curTick);
	}
}
